from Logger import logger

class PromptTemplate:
    def __init__(self, str_template=None):
        self.template = str_template

    def from_string(self, str_template):
        self.template = str_template
        return self

    def from_file(self, filename):
        f = open(filename, 'r')
        self.template = f.read()
        f.close()
        return self

    def get_prompt(self, **kwargs):
        prompt = self.template
        for key in kwargs:
            place_holder = f"__{key.upper()}_PLACEHOLDER__"
            if place_holder not in prompt:
                raise Exception(f"Key {place_holder} not found in template")
            prompt = prompt.replace(place_holder, str(kwargs[key]))
        return prompt

    def __str__(self):
        return self.template
